<template>
  <div>
    <page-dialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <!-- 条件栏 -->
      <page-filter
        :query="filterInfo.query"
        :filter-list="filterInfo.list"
      />
      <page-table
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="api"
        :listen-height="false"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :highlight-row="highlightRow"
        :check-box="checkBox"
        @handleEvent="handleEvent"
      />
    </page-dialog>
  </div>
</template>

<script setup name="selectApplicationUser">
import PageDialog from "../PageDialog/index";
import PageTable from "../PageTable/index";
import PageFilter from "../PageFilter/index";
import { getUserPage } from "@/api/base.js";
import { getDeptAll } from "@/api/core";

const { proxy } = getCurrentInstance();
const emit = defineEmits(["update", "callback", "reset"]);
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getUserPage,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {};
    },
  },
});

// 过滤相关配置
const filterInfo = reactive({
  query: {
    sort: "create_time desc",
    ...props.query,
  },
  list: [
    {
      type: "input",
      label: "名称",
      value: "nameLike",
      event: search,
    },
    {
      type: "slip-select",
      label: "部门",
      value: "deptIdMult",
      api: getDeptAll,
      multiple: true,
      event: search,
    },
    { type: "button", label: "搜索", event: search },
  ],
});
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: "名称", value: "name" },
    { label: "账号", value: "userName" },
    { label: "状态", value: "statusValue" },
    { label: "部门", value: "deptName" },
  ],
});
// 弹窗相关
const dialogInfo = reactive({
  title: "选择用户",
  btLoading: false,
  btList: [
    { label: "关闭", event: handleClose, show: true },
    { label: "清除", event: clear, show: true },
    {
      label: "保存",
      type: "primary",
      event: save,
      show: true,
    },
  ],
});
const selectRow = ref("");
const shows = computed({
  get() {
    return props.showDialog;
  },
  set(newValue) {
    emit("update:showDialog", newValue); // 更新父组件shows
  },
});

onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList();
  });
});

// 获取列表
function getList () {
  tableInfo.refresh = Math.random();
};
function search () {
  tableInfo.initCurpage = Math.random();
  tableInfo.refresh = Math.random();
};
function save () {
  // emit('input', data)
  if (selectRow.value) {
    handleClose();
    emit("callback", selectRow.value);
  } else {
    proxy.$message({
      type: "warning",
      duration: 3500,
      showClose: true,
      message: "请选择用户",
    });
  }
};
function clear () {
  handleClose();
  emit("callback", { id: "", applicationUserName: "" });
};
function handleEvent (event, data) {
  switch (event) {
    case "tableClickRow":
      selectRow.value = data;
      dialogInfo.btList[1].disabled = !selectRow.value;
      break;
    case "tableCheck":
      if (data.length) {
        selectRow.value = data;
        dialogInfo.btList[1].disabled = !selectRow.value;
      }
      break;
  }
};
// 关闭弹窗前的回调
function handleClose () {
  shows.value = false;
  emit("reset");
};
</script>

<style scoped>
:deep(.el-dialog__body) {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
